#include "Miner.h"
#include "GameMap.h"

Miner::Miner(GridVec pos, int name, int direction) : Building(pos, name, direction)
{
    FirstRequire_ms = 2700;
    SecondRequire_ms = 1500;
}

bool Miner::CanReceive(GridVec target, int directionin, int shapename)
{
    return false;
}

std::vector<GridVec> Miner::BuildingAllPos()
{
    std::vector<GridVec> result = {pos};
    return result;
}

bool Miner::IsPlace(const GridVec &click, int picdirection, GameMap &gamemap)
{
    // 如果超出地图范围，返回false
    if (click.i < 0 || click.i >= GameInfo::HEIGHT || click.j < 0 || click.j >= GameInfo::WIDTH)
    {
        return false;
    }
    // 如果不是在矿地上，返回false
    if (gamemap.GetResource(click) == NONE || gamemap.GetResource(click) == Goods::BARRIER)
    {
        return false;
    }
    return true;
}

int Miner::GetDigShapeType(GridVec pos, GameMap &gamemap)
{
    return gamemap.GetResource(pos);
}


void Miner::TickableRunning()
{
    timer.UpdateRuningTime(FirstRequire_ms);
    running_ms = timer.runningMs;
    return;
}

void Miner::UpdateTickableState(GameMap &gamemap)
{
    switch (state)
    {
    case TickableStatus::EMPTY:
        state = TickableStatus::RUNNING;
        running_ms = 0;
        shape.name = GetDigShapeType(pos, gamemap);
        timer.Reset();
        break;
    case TickableStatus::RUNNING:
        if (running_ms >= FirstRequire_ms)
        {
            // dig好了，准备运输
            state = TickableStatus::BLOCK;
            running_ms = 0;
        }
        else
        {
            TickableRunning();
        }
        break;
    case TickableStatus::BLOCK:
        if (CanSend(pos, direction, shape.name, gamemap))
        {
            Send(pos, direction, shape.name, gamemap);
            state = TickableStatus::EMPTY;
            shape.name = NONE;
        }
        break;
    default:
        break;
    }
    return;
}

void Miner::Receive(GridVec target, int directionin, int shapename)
{
    return;
}
